package br.gov.cemaden.pcd;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Hello world!
 *
 */
public class PCDData 
{
    
    public static void main( String[] args )
    {
        
        String path = null;        
        int year;
        int month;
        String from = null;
        String to = null;
        
        Integer givenMonth = null;
        Integer givenYear = null;
        
        Calendar calendar = Calendar.getInstance();        
        calendar.add(Calendar.DATE, -1);
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        int currentMonth = (calendar.get(Calendar.MONTH) + 1);
        int currentYear = calendar.get(Calendar.YEAR);       
        
        month = currentMonth;
        year = currentYear;
        
        String help = String.format("The path to save your files is a mandatory paramter%nUsage:%njava -jar [app-name.jar] [path-to-save-files] [month] [year]");
        
        if(args.length < 1){
            System.out.println(help);
            System.exit(0);
        }
        else if(args.length > 0){
            if(Files.exists(Paths.get(args[0]))){
                path = args[0];
                System.out.println(path);
            }
            else{
                System.err.printf("You must pass a valid path.%n");
                System.exit(0);
            }
            if(args.length >= 3){
                if(args[1] != null && args[2] != null){

                    givenMonth = Integer.parseInt(args[1]);
                    givenYear = Integer.parseInt(args[2]);
                    
                    if(givenYear.toString().length() != 4){
                        System.err.printf("You must pass a valid year.%n%s", help);
                        System.exit(0);
                    }

                    if(givenMonth < 1 || givenMonth > 12)
                        givenMonth = null;
                    if(givenYear.toString().length() != 4 || givenYear > currentYear)
                        givenYear = null;
                    if(givenYear.equals(currentYear)){
                        if(givenMonth > currentMonth)
                            givenMonth = null;
                    }
                }
            }
        }                                
        
        if(givenMonth != null && givenYear != null){
            
            if((givenMonth.equals(currentMonth)) && (givenYear.equals(currentYear))){
                from = String.format("%d-%d-01 00:00:00", currentYear, currentMonth);
                to = String.format("%d-%d-%d 23:59:59", currentYear, currentMonth, currentDay);
            }
            else{                                
                
                Calendar cal = new GregorianCalendar(givenYear, (givenMonth-1), 1);
                int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
                
                if(givenMonth.toString().length() == 1){
                    from = String.format("%d-0%d-01 00:00:00", givenYear, givenMonth);
                    to = String.format("%d-0%d-%d 23:59:59", givenYear, givenMonth, days);
                }
                else{
                    from = String.format("%d-%d-01 00:00:00", givenYear, givenMonth);
                    to = String.format("%d-%d-%d 23:59:59", givenYear, givenMonth, days);
                }                                
                
                year = givenYear;
                month = givenMonth;
            }
        }
        else{
            from = String.format("%d-%d-01 00:00:00", currentYear, currentMonth);
            to = String.format("%d-%d-%d 23:59:59", currentYear, currentMonth, currentDay);
        }
                      
        new ExportData(path, year, month, from, to).doExport();
    }    
}
